package com.scalar.chiptrack.operations.process.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.process.BoardMaking;
import com.scalar.chiptrack.operations.process.ProcessStep;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.dao.KitMakingDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.VendorLocationDAO;
import com.scalar.chiptrack.operations.process.forms.BoardKitForm;


public final class KitMakingAction extends ChipTrackAction
{


    private HttpSession mSession = null;

    public ActionForward execute ( ActionMapping mapping , ActionForm form ,
                                   HttpServletRequest request , HttpServletResponse response ) throws Exception
    {
        mSession = request.getSession ();
        String action = request.getParameter ( "action" );
        if ( action == null )
        {
            action = "";
        }

        BoardKitForm kitMakingForm = ( BoardKitForm ) form;
        HashMap info = new HashMap ();
        HashMap errors = new HashMap ();
        ArrayList  processTypeList = null;
        if ( ( action != null ) && ( action.equals ( "BoardMaking" ) ) )
        {
            request.setAttribute("processTypeList", kitMakingForm.getboardProcessList());
            request.setAttribute ( "Boards" , KitMakingDAO.loadAllBoardsAndKits (kitMakingForm.getProcessType()) );
            mSession.setAttribute( "kitActionType" , "" );
            mSession.setAttribute ( "Testing" , "Some value" );
            kitMakingForm.setProductId(-1);
            request.setAttribute( "SchemaFileDetailsList" , new BoardKitForm ().getSchematicFileDetailsList() );
            request.setAttribute( "BomRevFileDetailsList" , new BoardKitForm ().getBomRevFileDetailsList() );

            String poduct_Id = "" + kitMakingForm.getProductId ();
            if( poduct_Id.equals ( "0" ) )
            {
                errors.put( "Please select a valid Part Number", null );
                request.setAttribute ( SessionKeys.ERROR_MESSAGES , errors );
            }

            return ( mapping.findForward ( "boardMaking" ) );
        }
        else if ( ( action != null ) && ( action.equals ( "processType" ) ) )
        {
             mSession.setAttribute ( "kitMakingForm" , new BoardKitForm () );
             processTypeList = kitMakingForm.getboardProcessList();
             request.setAttribute("processTypeList", processTypeList);

            /*request.setAttribute ( "Boards" , KitMakingDAO.loadAllBoardsAndKits () );
            mSession.setAttribute( "kitActionType" , "" );
            mSession.setAttribute ( "Testing" , "Some value" );
            mSession.setAttribute ( "kitMakingForm" , new BoardKitForm () );
            request.setAttribute( "SchemaFileDetailsList" , new BoardKitForm ().getSchematicFileDetailsList() );
            request.setAttribute( "BomRevFileDetailsList" , new BoardKitForm ().getBomRevFileDetailsList() );

            //Added by Kumaraswamy on Sep 30th, 2004.
            String poduct_Id = "" + kitMakingForm.getProductId ();
            if( poduct_Id.equals ( "0" ) )
            {
                errors.put( "Please select a valid Part Number", null );
                request.setAttribute ( SessionKeys.ERROR_MESSAGES , errors );
            }
*/
            return ( mapping.findForward ( "boardMaking" ) );
        }
        else if ( ( action != null ) && ( action.equals ( "OpenBaordMaking" ) ) )
        {
            String psType = kitMakingForm.getProcessType();
            request.setAttribute("processTypeList", kitMakingForm.getboardProcessList());
            String productId = "" + kitMakingForm.getProductId ();
            request.setAttribute ( "Boards" , KitMakingDAO.loadAllBoardsAndKits (kitMakingForm.getProcessType()) );
            kitMakingForm = KitMakingDAO.loadBoardDetails ( productId.trim () );
            if ( productId.equals ( "-2" ) )
            {
                mSession.setAttribute( "kitActionType" , "" );
                mSession.setAttribute( "SchemaListSize" , "0");
                mSession.setAttribute( "BomRevListSize" , "0");
                kitMakingForm.setProductId ( -2 );
                kitMakingForm.setUnitPrice ( "0.00" );
                kitMakingForm.setProductType ( "BOARD" );
                kitMakingForm.setChildBoard ( getChildBoardDetails ( "-2" ) );//called the private method
                kitMakingForm.setBomRevId ( "-1" );
                kitMakingForm.setSchematicId ( "-1" );
                kitMakingForm.setBomRevNumber ( "" );
                kitMakingForm.setSchematicNumber ( "" );
                kitMakingForm.setProcessType(psType);
                request.setAttribute ( "blankChild" , "TRUE" );
            }
            else
            {
                 mSession.setAttribute( "kitActionType" , "modify" );
                request.setAttribute ( "blankChild" , "FALSE" );
                ArrayList tempChildBoard = KitMakingDAO.getChildBoardOrKitDetails ( kitMakingForm.getProductId () );
                if ( ( tempChildBoard == null ) || ( tempChildBoard.size () == 0 ) )
                    kitMakingForm.setChildBoard ( getChildBoardDetails ( "-2" ) );
                else
                    kitMakingForm.setChildBoard ( tempChildBoard );

                kitMakingForm.setPcbRevNo ( KitMakingDAO.loadPcbRevNumber ( kitMakingForm.getProductId () ) );
            }
            // added by saravnan
            /*kitMakingForm.setBomRevFileDetails ( "" );
            kitMakingForm.setSchematicFileDetails ( "" );
            kitMakingForm.setSchematicFileList ( new ArrayList () );
            kitMakingForm.setBomRevFileList ( new ArrayList () );

            BomRevOrSchmFileList  schemaFileDetailsList = KitMakingDAO.loadSchemaFileDetails ( kitMakingForm.getProductId () , kitMakingForm.getProductType () );
            BomRevOrSchmFileList  bomRevFileDetailsList = KitMakingDAO.loadBomRevFileDetails ( kitMakingForm.getProductId () , kitMakingForm.getProductType () );
            kitMakingForm.setBomRevFiles ( bomRevFileDetailsList );
            kitMakingForm.setSchmaFiles (  schemaFileDetailsList);
            if( (bomRevFileDetailsList.getFileList() ).size() == 0 )
            {
                mSession.setAttribute( "BomRevListSize" , "0");
            }
            else
            {
                mSession.setAttribute( "BomRevListSize" , "1");
            }
            if( (schemaFileDetailsList.getFileList() ).size() == 0 )
            {
               mSession.setAttribute( "SchemaListSize" , "0");
            }
            else
            {
                mSession.setAttribute( "SchemaListSize" , "1");
            }
            */
            kitMakingForm.setProcessType(psType);

            //request.setAttribute( "SchemaFileDetailsList" ,  kitMakingForm.getSchematicFileDetailsList() );
            //request.setAttribute( "BomRevFileDetailsList" , kitMakingForm.getBomRevFileDetailsList() );
            mSession.setAttribute ( "kitMakingForm" , kitMakingForm );
            request.setAttribute ( "showDetails" , "TRUE" );
            return ( mapping.findForward ( "boardMaking" ) );
        }
        else if ( ( action.equals ( "SaveBoardMaking" ) ) )
        {
            //request.setAttribute( "BomRevFileDetailsList" , kitMakingForm.getBomRevFileDetailsList() );
            //request.setAttribute( "SchemaFileDetailsList" , kitMakingForm.getSchematicFileDetailsList() );

            DBTransaction dbTransaction = null;
            boolean success 	= false;
            int processStepId 	= -1;
            int productId 		= -1;
			int isApproved 		= 1;
			String STEP_TYPE 	= kitMakingForm.getProcessType();
            String productType 	= kitMakingForm.getProductType ();
			String pcbRevNum 	= kitMakingForm.getPcbRevNo ();
			String unitPrice 	= kitMakingForm.getUnitPrice ();
			String partNumber 	= kitMakingForm.getPartNumber ().trim ();
			String mktPartNumber = null;
			String description 	= "";
            ArrayList locations = getVendorLocations ( request );
            ArrayList childBoards 	= getChildBoardDetails ( request );
            ArrayList selectedVendorLocations = new ArrayList();
			boolean app = kitMakingForm.getIsApproved ();
			
			kitMakingForm.setVendorLocations ( locations );
            kitMakingForm.setChildBoard ( childBoards );
			
			if ( kitMakingForm.getProcessStepId () != -1 )
			{
				processStepId = kitMakingForm.getProcessStepId ();
			}
            
			if ( kitMakingForm.getProductId () != -1 )
			{
				productId = kitMakingForm.getProductId ();
			}
			if ( kitMakingForm.getDescription () != null )
			{
				description = kitMakingForm.getDescription ();
			}
			
			if( kitMakingForm.getProcessType().equalsIgnoreCase( "FINISHED GOODS" ) )
			{
				mktPartNumber = partNumber;
			}
			
			selectedVendorLocations = getVendorLocations( request, true );
			
            if ( app == true )
                isApproved = 0;

            try
            {
                // start transaction
                dbTransaction = DBTransaction.getInstance ();
                dbTransaction.begin ();

                if ( processStepId == -1 )
                {
                    // Step 1
                    // Check for the Part Number already Exists
                    mSession.setAttribute( "kitActionType" , "" );
                    String tempproductId = ProductDao.getProductId( dbTransaction, partNumber, STEP_TYPE, true );
                    
                    if ( tempproductId != null && "".equalsIgnoreCase ( tempproductId.trim() ) == false )
                    {
                        errors.put ( "Same Part Number " + partNumber + " already Present" , " Please Enter Another PartNumber" );
                        throw new ScalarException ( "Same Part Number already Present" );
                    }

                    // Step 2
                    // Check for partnumber in Product map table
                    String[] partNumbers = new String[ 1 ];
                    partNumbers[ 0 ] = partNumber;

                    String dupNames = ProductDao.checkDupInProductMapTable ( partNumbers, processStepId, STEP_TYPE );
                        
                    if( dupNames != null && dupNames.length () > 0 )
                    {
                        errors.put ( "Same Part Number " + partNumber + " already Present" , " Please Enter Another PartNumber" );
                        throw new ScalarException ( "Same Part Number already Present" );
                    }

                    // Step 3
                    // for Checking the Duplicate part numbers for the Child boards
                    if ( childBoards != null && childBoards.size () > 0 )
                    {
                        checkForDuplicationInChildPartNumbers ( dbTransaction , childBoards , processStepId, productId , errors , request , STEP_TYPE , true );
                    }

                    processStepId = ProcessStep.getProcessStepID ( dbTransaction , STEP_TYPE , STEP_TYPE , description );
                    
                    // Step 4
                    // Store Product Table information
                    productId = ProductDao.saveProcessStepProduct ( dbTransaction ,
                            processStepId ,
                            partNumber ,
                            -1 ,
                            mktPartNumber ,
                            description ,
                            isApproved + "" ,
                            productType
                    );
                    
                    // Step 6
                    // save the price into Product Price table
                    KitMakingDAO.saveBoardPrice ( dbTransaction , productId , kitMakingForm.getUnitPrice () , true ); //true for insert

                    // Step 7
                    // Save part number in Inventory Step Table
                    KitMakingDAO.saveBoardinInventory ( dbTransaction , partNumber , processStepId , true ); //true is for insert
                    
                    // Step 8
                    // Save vendor locations
                    if ( ( locations != null ) && ( locations.size () > 0 ) )
                    {
                        VendorLocationDAO.saveVendorLocations ( dbTransaction , processStepId , selectedVendorLocations.iterator () );
                    }

                    // Step 9
                    //save the file and pcb rev number details
                    //KitMakingDAO.insertPcbBomSchematicValues ( dbTransaction , kitMakingForm , productId , true );

                    // Step 10
                    // insert child boards
                    if ( childBoards != null && childBoards.size () > 0 )
                    {
                        KitMakingDAO.saveChildBoardDetails ( dbTransaction , childBoards , description , productType , productId , isApproved , STEP_TYPE );
                    }
                }
                else
                {
                    // Step 1
                    // to check whether another PartNumber is available with same partNumber
                    mSession.setAttribute( "kitActionType" , "modify" );
                    String tempproductId = ProductDao.getProductId ( dbTransaction , partNumber , true );
                    if ( tempproductId == null ) tempproductId = "";
                    tempproductId = tempproductId.trim ();
                    if ( ( tempproductId != "" ) && ( tempproductId.equalsIgnoreCase ( productId + "" ) == false ) )
                    {
                        errors.put ( "Same Part Number " + partNumber + " already Present" , " Please Enter Another PartNumber" );

                         return ( mapping.findForward ( "boardMaking" ) );
                    }

                    //step 2
                    // To check in Product map table

                    String[] partNumbers = new String[ 1 ];
                    partNumbers[ 0 ] = partNumber;
                    String dupNames = ProductDao.checkDupInProductMapTable ( partNumbers , processStepId , STEP_TYPE );
                    String originalPartNumber = request.getParameter ( "originalPartNumber" );
                    if ( dupNames == null )
                        dupNames = "";
                    if ( ( dupNames != "" ) && ( dupNames.equalsIgnoreCase ( originalPartNumber ) == false ) )
                    {
                        errors.put ( "Same Part Number " + partNumber + " already Present" , " Please Enter Another PartNumber" );

                        throw new ScalarException ( "Same Part Number already Present" );

                    }

                    // Step3
                    // To check child Part numbers
                    if ( childBoards != null && childBoards.size () > 0 )
                    {
                        checkForDuplicationInChildPartNumbers ( dbTransaction , childBoards , processStepId, productId , errors , request , STEP_TYPE , false );
                    }

                    //Step 4
                    //Update Process Step Table
                    ProcessStep.updateProcessStepID ( dbTransaction , STEP_TYPE , STEP_TYPE , description , processStepId );

                    // Step 5
                    // update product type and Description
                    KitMakingDAO.updateProductTypeAndDescription ( dbTransaction , productId , productType , description , partNumber , partNumber , isApproved );

                    //Step 6
                    // save Board price
                    KitMakingDAO.saveBoardPrice ( dbTransaction , productId , kitMakingForm.getUnitPrice () , false ); //false for update

                    // Step 7
                    // Save part number in Inventory Step Table
                    KitMakingDAO.saveBoardinInventory ( dbTransaction , partNumber , processStepId , false ); //false for update

                    // Step 8
                    // Delete and Save new Vendor Locations
                    VendorLocationDAO.getLocationsOfRunningLots ( dbTransaction , processStepId , selectedVendorLocations , errors );

                    VendorLocationDAO.deleteVendorLocations ( dbTransaction , processStepId );
                    if ( ( locations != null ) && ( locations.size () > 0 ) )
                    {
                        VendorLocationDAO.saveVendorLocations ( dbTransaction , processStepId , selectedVendorLocations.iterator () );
                    }

                    // Step 9
                    // added saravnan
                    //KitMakingDAO.insertPcbBomSchematicValues ( dbTransaction , kitMakingForm , productId , true );
                    // commented by saravnan
                    //KitMakingDAO.insertPcbBomSchematicValues ( dbTransaction , kitMakingForm , productId , false );

                    // Step 10
                    // update/insert child boards
                    if ( childBoards != null && childBoards.size () > 0 )
                    {
                        KitMakingDAO.updateChildBoardDetails ( dbTransaction , childBoards , description , productType , productId , isApproved , STEP_TYPE );
                    }

                }
                success = true;
            }
            catch ( ScalarException se )
            {
                success = false;
                request.setAttribute ( SessionKeys.ERROR_MESSAGES , errors );
            }
            catch ( Exception se )
            {
                errors.put ( productType + " Details" , "Problem in saving details" );
                se.printStackTrace ();
                request.setAttribute ( SessionKeys.ERROR_MESSAGES , errors );
            }
            finally
            {
                // Rollback or Commit the Transaction
                if ( success == true )
                {
                    if ( dbTransaction != null )
                    {
                        dbTransaction.commit ();
                    }
                    if("BOARD".equalsIgnoreCase(productType) ){
                        info.put ( "Board Details" , partNumber + " saved successfully" );
                    }else{
                        info.put ( "Kit Details" , partNumber + " saved successfully" );
                    }
                }
                else
                {
                    dbTransaction.rollback ();
                }
            }

            if ( success )
            {
                kitMakingForm.setProductId ( -1 );
                request.setAttribute ( "action" , "BoardMaking" );
                formReset ( kitMakingForm );
                request.setAttribute ( "showDetails" , "FALSE" );
            }
            else
            {
                kitMakingForm.setUnitPrice ( unitPrice );
                kitMakingForm.setIsApproved ( app );
                kitMakingForm.setProductType ( productType );
                kitMakingForm.setDescription ( description );

                if ( childBoards == null || childBoards.size () == 0 )
                    kitMakingForm.setChildBoard ( getChildBoardDetails ( "-2" ) );
                else
                    kitMakingForm.setChildBoard ( childBoards );
                kitMakingForm.setPcbRevNo ( pcbRevNum );
                kitMakingForm.setVendorLocations ( locations );
                if ( productId == -2 )
                {
                    kitMakingForm.setProductId ( -2 );
                    kitMakingForm.setPartNumber ( partNumber );
                }
                else
                {
                    kitMakingForm.setProductId ( productId );
                    kitMakingForm.setPartNumber ( partNumber );
                }
                request.setAttribute ( "showDetails" , "TRUE" );
            }
                        
            request.setAttribute ( SessionKeys.INFO_MESSAGES , info );
            request.setAttribute("processTypeList", kitMakingForm.getboardProcessList());
            return ( mapping.findForward ( "boardMaking" ) );
        }

        // added by saravnan process addmore file action
        /*else if ( action.equals ( "addmoreBomRevFiles" ) || action.equals ( "addmoreSchemFiles" ) )
        {
            if ( action.equals ( "addmoreBomRevFiles" ) )
            {
                FormFile fFile = kitMakingForm.getBomRevNo ();
                BomRevSchemaFile bomRevSchemaFile = new BomRevSchemaFile ();
                bomRevSchemaFile.setFormFile ( fFile );
                bomRevSchemaFile.setFileName( fFile.getFileName() );
                bomRevSchemaFile.setBomOrSchemaNumber ( kitMakingForm.getBomRevNumber () );

                ArrayList tempFileList = kitMakingForm.getBomRevFileList ();
                ArrayList fileNameList =  kitMakingForm.getBomRevFileDetailsList();
                if( !("".equals( fFile.getFileName() ) ) && fFile.getFileName() != null && fFile.getFileName().length() != 0 && fFile.getFileSize() != 0 )
                {
                    tempFileList.add ( bomRevSchemaFile );
                    fileNameList.add( new LabelValueBean( fFile.getFileName().trim () , "" ) );
                    kitMakingForm.setBomRevFileDetailsList( fileNameList );
                    kitMakingForm.setBomRevFileList ( tempFileList );
                }
                else
                {
                    errors.put( "The file requested to attach is empty or does not exists", null );
                    request.setAttribute ( SessionKeys.ERROR_MESSAGES , errors );
                }
                request.setAttribute( "BomRevFileDetailsList" , fileNameList );
                request.setAttribute( "SchemaFileDetailsList" , kitMakingForm.getSchematicFileDetailsList() );

                kitMakingForm.setBomRevNo ( null );
                kitMakingForm.setBomRevNumber ( "" );
            }
            else if ( action.equals ( "addmoreSchemFiles" ) )
            {

                String SchemFileDetails = "";
                FormFile fFile = kitMakingForm.getSchematic ();
                BomRevSchemaFile schemaFile = new BomRevSchemaFile ();
                schemaFile.setFormFile ( fFile );
                schemaFile.setFileName( fFile.getFileName() );
                schemaFile.setBomOrSchemaNumber ( kitMakingForm.getSchematicNumber () );

                ArrayList tempFileList = kitMakingForm.getSchematicFileList ();
                ArrayList fileNameList =  kitMakingForm.getSchematicFileDetailsList();
                if( !("".equals( fFile.getFileName() ) ) && fFile.getFileName() != null && fFile.getFileName().length() != 0 && fFile.getFileSize() != 0 )
                {
                    tempFileList.add ( schemaFile );
                    fileNameList.add( new LabelValueBean( fFile.getFileName().trim () , "" ) );
                    kitMakingForm.setSchematicFileDetailsList( fileNameList );
                    kitMakingForm.setSchematicFileList ( tempFileList );
                }
                else
                {
                    errors.put( "The file requested to attach is empty or does not exists", null );
                    request.setAttribute ( SessionKeys.ERROR_MESSAGES , errors );
                }
                request.setAttribute( "SchemaFileDetailsList" , fileNameList );
                request.setAttribute( "BomRevFileDetailsList" , kitMakingForm.getBomRevFileDetailsList() );

                kitMakingForm.setSchematic ( null );
                kitMakingForm.setSchematicNumber ( "" );
            }
			kitMakingForm.setBomRevFileDetails( "" );
            kitMakingForm.setSchematicFileDetails( "" );

            ArrayList locations = getVendorLocations ( request );
            kitMakingForm.setVendorLocations ( locations );
            ArrayList childBoards = getChildBoardDetails ( request );
            kitMakingForm.setChildBoard ( childBoards );
            request.setAttribute ( "childBoard" , childBoards );

            int productId = kitMakingForm.getProductId ();
            if ( childBoards == null || childBoards.size () == 0 )
            {
                kitMakingForm.setChildBoard ( getChildBoardDetails ( "-2" ) );
            }
            else
            {
                if ( productId == -2 )
                {
                    kitMakingForm.setProductId ( -2 );
                }
                else
                {
                    kitMakingForm.setProductId ( productId );
                }

            }
            request.setAttribute ( "showDetails" , "TRUE" );
            request.setAttribute("processTypeList", kitMakingForm.getboardProcessList());
            request.setAttribute ( "Boards" , KitMakingDAO.loadAllBoardsAndKits (kitMakingForm.getProcessType()) );
            mSession.setAttribute ( "kitMakingForm" , kitMakingForm );
            return ( mapping.findForward ( "boardMaking" ) );
        }
        // added by saravnan process remove file action
        else if ( action.equals ( "removeBomRevFiles" ) || action.equals ( "removeSchemFiles" ) )
        {
            if ( action.equals ( "removeBomRevFiles" ) )
            {
                boolean flag = false;
                ArrayList tempFileList = kitMakingForm.getBomRevFileList ();
                ArrayList fileNameList =  kitMakingForm.getBomRevFileDetailsList();
                for( int i =0 ; i < tempFileList.size() ; i++ )
                {
                    BomRevSchemaFile bomRevSchemaFile = ( BomRevSchemaFile  )tempFileList.get( i );
                    if(  bomRevSchemaFile.getFileName().equals( kitMakingForm.getBomRevFileDetails() )  )
                    {
                        flag = true;
                    }
                    if( flag == true )
                    {
                        tempFileList.remove( i );
                        break;
                    }
                }
                flag = false;
                for( int i =0 ; i < fileNameList.size() ; i++ )
                {
                    LabelValueBean lb = ( LabelValueBean ) fileNameList.get( i );
                    String bomRevSchemaName = lb.getLabel();
                    if(  bomRevSchemaName.equals( kitMakingForm.getBomRevFileDetails() )  )
                    {
                        flag = true;
                    }
                    if( flag == true )
                    {
                        fileNameList.remove( i );
                        break;
                    }
                }

                kitMakingForm.setBomRevFileDetailsList( fileNameList );
                kitMakingForm.setBomRevFileList ( tempFileList );
                request.setAttribute( "BomRevFileDetailsList" , fileNameList );
                request.setAttribute( "SchemaFileDetailsList" , kitMakingForm.getSchematicFileDetailsList() );

                kitMakingForm.setBomRevNo ( null );
                kitMakingForm.setBomRevNumber ( "" );
                kitMakingForm.setBomRevFileDetails( "" );
                kitMakingForm.setSchematicFileDetails( "" );
            }
            else if ( action.equals ( "removeSchemFiles" ) )
            {

                ArrayList tempFileList = kitMakingForm.getSchematicFileList ();
                ArrayList fileNameList =  kitMakingForm.getSchematicFileDetailsList();
                for( int i =0 ; i < tempFileList.size() ; i++ )
                {
                    BomRevSchemaFile schemaFile = ( BomRevSchemaFile  )tempFileList.get( i );
                    if(  schemaFile.getFileName().equals( kitMakingForm.getSchematicFileDetails() )  )
                    {
                        tempFileList.remove( i );
                        break;
                    }
                }

                for( int i =0 ; i < fileNameList.size() ; i++ )
                {
                    LabelValueBean lb = ( LabelValueBean ) fileNameList.get( i );
                    String schemaName = lb.getLabel();
                    if(  schemaName.equals( kitMakingForm.getSchematicFileDetails() )  )
                    {
                        fileNameList.remove( i );
                        break;
                    }

                }
                kitMakingForm.setSchematicFileDetailsList( fileNameList );
                kitMakingForm.setSchematicFileList ( tempFileList );
                request.setAttribute( "SchemaFileDetailsList" , fileNameList );
                request.setAttribute( "BomRevFileDetailsList" , kitMakingForm.getBomRevFileDetailsList() );

                kitMakingForm.setSchematic ( null );
                kitMakingForm.setSchematicNumber ( "" );
                kitMakingForm.setSchematicFileDetails( "" );
            }

            ArrayList locations = getVendorLocations ( request );
            kitMakingForm.setVendorLocations ( locations );
            ArrayList childBoards = getChildBoardDetails ( request );
            kitMakingForm.setChildBoard ( childBoards );
            request.setAttribute ( "childBoard" , childBoards );

            int productId = kitMakingForm.getProductId ();
            if ( childBoards == null || childBoards.size () == 0 )
            {
                kitMakingForm.setChildBoard ( getChildBoardDetails ( "-2" ) );
            }
            else
            {
                if ( productId == -2 )
                {
                    kitMakingForm.setProductId ( -2 );
                }
                else
                {
                    kitMakingForm.setProductId ( productId );
                }

            }
            request.setAttribute ( "showDetails" , "TRUE" );
            request.setAttribute("processTypeList", kitMakingForm.getboardProcessList());
            request.setAttribute ( "Boards" , KitMakingDAO.loadAllBoardsAndKits (kitMakingForm.getProcessType()) );
            mSession.setAttribute ( "kitMakingForm" , kitMakingForm );
            return ( mapping.findForward ( "boardMaking" ) );
        }
        else if ( action.equals ( "ModifyBomRevFiles" ) || action.equals ( "ModifySchemFiles" ) )
        {
            if ( action.equals ( "ModifyBomRevFiles" ) )
            {

                String bomRevNumber = kitMakingForm.getBomRevNumber().trim();
                ArrayList tempFileList = kitMakingForm.getBomRevFileList ();
                for( int i =0 ; i < tempFileList.size() ; i++ )
                {
                    BomRevSchemaFile bomRevSchemaFile = ( BomRevSchemaFile  )tempFileList.get( i );
                    if(  bomRevSchemaFile.getFileName().equals( kitMakingForm.getBomRevFileDetails() )  )
                    {
                        bomRevSchemaFile.setBomOrSchemaNumber( bomRevNumber );
                        tempFileList.remove( i );
                        tempFileList.add( i , bomRevSchemaFile );
                        break;
                    }

                }
                kitMakingForm.setBomRevFileList ( tempFileList );
                request.setAttribute( "BomRevFileDetailsList" , kitMakingForm.getBomRevFileDetailsList() );
                request.setAttribute( "SchemaFileDetailsList" , kitMakingForm.getSchematicFileDetailsList() );

                kitMakingForm.setBomRevNo ( null );
                kitMakingForm.setBomRevNumber ( "" );
            }
            else if ( action.equals ( "ModifySchemFiles" ) )
            {
                String schemaNumber = kitMakingForm.getSchematicNumber().trim();
                ArrayList tempFileList = kitMakingForm.getSchematicFileList ();

                for( int i =0 ; i < tempFileList.size() ; i++ )
                {
                    BomRevSchemaFile schemaFile = ( BomRevSchemaFile  )tempFileList.get( i );
                    if(  schemaFile.getFileName().equals( kitMakingForm.getSchematicFileDetails() )  )
                    {
                        schemaFile.setBomOrSchemaNumber( schemaNumber  );
                        tempFileList.remove( i );
                        tempFileList.add( i , schemaFile );
                        break;
                    }
                }
                kitMakingForm.setSchematicFileList ( tempFileList );
                request.setAttribute( "SchemaFileDetailsList" , kitMakingForm.getSchematicFileDetailsList());
                request.setAttribute( "BomRevFileDetailsList" , kitMakingForm.getBomRevFileDetailsList() );

                kitMakingForm.setSchematic ( null );
                kitMakingForm.setSchematicNumber ( "" );
            }
            kitMakingForm.setBomRevFileDetails( "" );
            kitMakingForm.setSchematicFileDetails( "" );

            ArrayList locations = getVendorLocations ( request );
            kitMakingForm.setVendorLocations ( locations );
            ArrayList childBoards = getChildBoardDetails ( request );
            kitMakingForm.setChildBoard ( childBoards );
            request.setAttribute ( "childBoard" , childBoards );

            int productId = kitMakingForm.getProductId ();
            if ( childBoards == null || childBoards.size () == 0 )
            {
                kitMakingForm.setChildBoard ( getChildBoardDetails ( "-2" ) );
            }
            else
            {
                if ( productId == -2 )
                {
                    kitMakingForm.setProductId ( -2 );
                }
                else
                {
                    kitMakingForm.setProductId ( productId );
                }

            }
            request.setAttribute ( "showDetails" , "TRUE" );
            request.setAttribute("processTypeList", kitMakingForm.getboardProcessList());
            request.setAttribute ( "Boards" , KitMakingDAO.loadAllBoardsAndKits (kitMakingForm.getProcessType()) );
            mSession.setAttribute ( "kitMakingForm" , kitMakingForm );
            return ( mapping.findForward ( "boardMaking" ) );
        }*/
        return null; // dump flow never comes never.
    }

    private ArrayList getVendorLocations ( HttpServletRequest request )
    {
        ArrayList vendorLocations = new ArrayList ();
        String[] locationName = request.getParameterValues ( "locationName" );
        if ( locationName.length == 0 )
        {
            return null;
        }
        String[] priority 	= request.getParameterValues ( "priority" );
        String[] locationID = request.getParameterValues ( "locationID" );
		String select[] = request.getParameterValues ( "select" );
        VendorLocation location = null;
        int selectCount = 0;

        for ( int i = 0 ; i < locationName.length ; i++ )
        {
            try
            {
                if ( priority[ i ].trim ().length () > 0 )
                //if ( ( select[i] != null ) && (select[i].equalsIgnoreCase("on") ) )
                {    // check if this row was enabled by the user or not
                    // save only if its enabled
                    location = new VendorLocation ( select[ selectCount ] , priority[ i ] ,
                            locationName[ i ] , locationID[ i ] );
                    vendorLocations.add ( location );
                    selectCount++;
                }
                else
                {
                    location = new VendorLocation ( "" , "" , locationName[ i ] , locationID[ i ] );
                    vendorLocations.add ( location );
                }
            }
            catch ( Exception ignore )
            {
            }
        }
        return vendorLocations;
    }

    private ArrayList getVendorLocations ( HttpServletRequest request , boolean bool )
    {
        ArrayList vendorLocations = new ArrayList ();
        String[] locationName = request.getParameterValues ( "locationName" );
        if ( locationName.length == 0 )
        {
            return null;
        }
        String[] priority = request.getParameterValues ( "priority" );
        String[] locationID = request.getParameterValues ( "locationID" );
        VendorLocation location = null;
        String select[] = request.getParameterValues ( "select" );
        int selectCount = 0;
        for ( int i = 0 ; i < locationName.length ; i++ )
        {

            try
            {
                if ( priority[ i ].trim ().length () > 0 )
                //if ( ( select[i] != null ) && (select[i].equalsIgnoreCase("on") ) )
                {    // check if this row was enabled by the user or not
                    // save only if its enabled
                    location = new VendorLocation ( select[ selectCount ] , priority[ i ] ,
                            locationName[ i ] , locationID[ i ] );
                    vendorLocations.add ( location );
                    selectCount++;
                }
            }
            catch ( Exception ignore )
            {
            }
        }
        return vendorLocations;
    }

    private ArrayList getChildBoardDetails ( String productId )
    {
        ArrayList childBoardDetails = new ArrayList ();
        BoardMaking boardMaking = new BoardMaking ();
        if ( productId.equals ( "-2" ) )
        {
            //The for loop given will add three rows by default.
            for ( int count = 0 ; count < 3 ; count++ )
            {
                boardMaking.setChildProductId ( "-1" );
                boardMaking.setChildBoardName ( "" );
                boardMaking.setOriginalChildBoardName ( "" );
                childBoardDetails.add ( boardMaking );
                boardMaking = new BoardMaking ();
            }
        }
        return childBoardDetails;
    }


    private ArrayList getChildBoardDetails ( HttpServletRequest request )
    {
        ArrayList childBoardDetails = new ArrayList ();
        BoardMaking boardMaking = new BoardMaking ();
        String[] childProductId = request.getParameterValues ( "childProductId" );
        if ( childProductId == null || childProductId.length == 0 )
        {
            return null;
        }
        String[] childBoardName = request.getParameterValues ( "childBoardName" );
        String[] originalChildBoardName = request.getParameterValues ( "originalChildBoardName" );
        int childProductCount = 0;
        for ( int i = 0 ; i < childBoardName.length ; i++ )
        {
            try
            {
                if ( childBoardName[ i ].trim ().length () == 0 )
                {
                    continue;
                }
                boardMaking.setChildProductId ( childProductId[ i ].trim () );
                boardMaking.setChildBoardName ( childBoardName[ i ].trim () );
                boardMaking.setOriginalChildBoardName ( originalChildBoardName[ i ].trim () );
                childBoardDetails.add ( boardMaking );
                boardMaking = new BoardMaking ();
            }
            catch ( Exception ignore )
            {
            }
        }
        return childBoardDetails;
    }


    private void checkForDuplicationInChildPartNumbers ( DBTransaction dbTransaction , ArrayList childPartNumbers , int processStepId , int parentProductId ,
                                                         HashMap errors , HttpServletRequest request , String STEP_TYPE , boolean insert ) throws ScalarException
    {
        ArrayList childPartNumber = childPartNumbers;
        BoardMaking boardMaking = null;
        String partNumber = "";
        String childProductId [] = request.getParameterValues ( "childProductId" );
        String childBoardName [] = request.getParameterValues ( "childBoardName" );
        String originalChildBoardName [] = request.getParameterValues ( "originalChildBoardName" );
        String tempproductId = null;

        for ( int count = 0 ; count < childPartNumber.size () ; count++ )
        {
            boardMaking = ( BoardMaking ) childPartNumber.get ( count );
            partNumber = boardMaking.getChildBoardName ();

            //Commented by Krishna Prasad on 12 Aug 2004
            //String tempproductId = ProductDao.getProductId ( dbTransaction , partNumber.trim () , true );

            //Added by Krishna Prasad on 12 Aug 2004
            tempproductId = null;

            if ( insert )
            {
                tempproductId = ProductDao.getProductId ( dbTransaction , partNumber.trim () , true );
            }
            else
            {
                tempproductId = KitMakingDAO.getProductId ( dbTransaction , partNumber.trim () , parentProductId );
            }
            //ends-here
            if ( tempproductId == null )
                tempproductId = "";
            tempproductId = tempproductId.trim ();
            //Check whether another part number with same name is already present in product table or not.
            if ( insert )
            {
                if ( "".equalsIgnoreCase ( tempproductId ) == false )
                {
                    errors.put ( "Same Child Board Number " + partNumber + " already exists" , " Please Enter Another Child Board Number" );
                    request.setAttribute("dupPartNumber", partNumber);
                    throw new ScalarException ( "Same Part Number already Present" );
                }
            }
            else
            {
                if ( ( tempproductId != "" ) && ( tempproductId.equalsIgnoreCase ( childProductId[ count ] + "" ) == false ) )
                {
                    errors.put ( "Same Child Board Number " + partNumber + " already exists" , " Please Enter Another Child Board Number" );
                    request.setAttribute("dupPartNumber", partNumber);
                    throw new ScalarException ( "Same Part Number already Present" );
                }
            }
        }


        if ( insert )
        {
            for ( int count = 0 ; count < childBoardName.length ; count++ )
            {
                //Check whether another part number with the same name is available in Product_Map_Table or not.
                String tempChildBoardName[] = {childBoardName[ count ].trim ()};
                String dupNames = ProductDao.checkDupInProductMapTable ( tempChildBoardName , processStepId , STEP_TYPE );
                if ( dupNames == null )
                    dupNames = "";
                if ( dupNames.length () > 0 )
                {
                    errors.put ( "Same Child Board Number " + dupNames + " already exists" , " Please Enter Another Child Board Number" );
                    request.setAttribute("dupPartNumber", dupNames);
                    throw new ScalarException ( "Same Part Number already Present" );
                }
            }
        }
        else
        {
            for ( int count = 0 ; count < childBoardName.length ; count++ )
            {
                //Commented by Krishna Prasad on 12 Aug 2004
                //Check whether another part number with the same name is available in Product_Map_Table or not.
                //String tempChildBoardName[] = {childBoardName[ count ].trim ()};
                //String dupNames = ProductDao.checkDupInProductMapTable ( tempChildBoardName , processStepId , STEP_TYPE );

                //Added by Krishna Prasad on 12 Aug 2004
                String tempChildBoardName =  childBoardName[ count ].trim () ;
                String dupNames = KitMakingDAO.checkDupInProductMapTable( dbTransaction , tempChildBoardName , processStepId , parentProductId );
                if ( dupNames == null )
                    dupNames = "";
                //Commented by Krishna Prasad on 12 Aug 2004
                //if ( ( dupNames != "" ) && ( dupNames.equalsIgnoreCase ( "" + originalChildBoardName[ count ] ) == false ) )
                if ( dupNames.length () > 0 )
                {
                    errors.put ( "Same Child Board Number " + tempChildBoardName + " already exists" , " Please Enter Another Child Board Number" );
                    request.setAttribute("dupPartNumber", tempChildBoardName);
                    throw new ScalarException ( "Same Part Number already Present" );
                }
            }
        }
    }

    // added by saravnan to reset the form
    public void formReset ( BoardKitForm kitMakingForm )
    {
        kitMakingForm.setProductId ( -1 );
        kitMakingForm.setProcessStepId ( -1 );
        kitMakingForm.setPartNumber ( null );
        kitMakingForm.setDescription ( null );
        kitMakingForm.setUnitPrice ( "" );
        kitMakingForm.setProductType ( null );
        kitMakingForm.setIsApproved ( false );
        kitMakingForm.setVendorLocations ( null );
        kitMakingForm.setPcbRevNo ( null );
        kitMakingForm.setBomRevId ( null );
        kitMakingForm.setSchematicId ( null );
        kitMakingForm.setBomRevNumber ( null );
        kitMakingForm.setSchematicNumber ( null );
        kitMakingForm.setBomRevNo ( null );
        kitMakingForm.setSchematic ( null );
        kitMakingForm.setChildBoard ( null );
        kitMakingForm.setBomRevFileList ( new ArrayList () );
        kitMakingForm.setBomRevFileDetails ( null );
        kitMakingForm.setSchematicFileList ( new ArrayList () );
        kitMakingForm.setSchematicFileDetails ( null );
        kitMakingForm.setBomDocNumber ( null );
        kitMakingForm.setSchematicDocId ( 0 );
        kitMakingForm.setSchematicDocNumber ( null );
        kitMakingForm.setBomFileNames ( null );
        kitMakingForm.setSchematicFileNames ( null );
        kitMakingForm.setIsBomFileDelete ( false );
        kitMakingForm.setIsSchematicDelete ( false );
        kitMakingForm.setBomDocNames ( null );
        kitMakingForm.setSchemDocNames ( null );
    }
}